<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Kotan blog controller containing actions of blog module.
 * @author bagnz0r
 * @package Kotan
 */

class Controller_Blog extends Controller_Kotan {
	
	/**
	 * Default action.
	 * Outputs all the blog posts available.
	 */
	public function action_index()
	{
		// Get posts
		$posts = ORM::factory('post')->find_all();
		
		// Get description length from configuration
		$desc_length = Kohana::$config->load('kotan.blog.desc_length');
		
		// Output data to the view
		$this->template->content = View::factory('blog/index/index')
										->bind('posts', $posts)
										->bind('desc_length', $desc_length);
	}
	
	public function action_view()
	{
		// Get the param
		if (!$id = $this->get_param('id', true))
			Request::current()->redirect('blog');
			
		// Get the post
		$post = ORM::factory('post', $id);
		
		// Check if we're not empty
		if ($post->id == '')
			throw new HTTP_Exception_404('Not found', array(0));
		
		// Output data to the view
		$this->template->content = View::factory('blog/view/index')
										->bind('post', $post);
	}
}